﻿using System;
using System.Collections.Generic;
using System.Configuration;
using Core.BOL;
using Core.DAL;
using Core.Utils;
using Elmah;

namespace Core.ScheduleTask
{
    public static class SendMailQueue
    {
        public static void Proceed()
        {
            using (MailQueueRepository mqr = new MailQueueRepository())
            {
                //Parameters
                int batchItems = int.Parse(ConfigurationManager.AppSettings["SendMailQueue.BatchItems"]);

                //Get datas
                List<tMail_Queue> lMails = mqr.GetMail(batchItems);

                foreach (tMail_Queue mail in lMails)
                {
                    try
                    {
                        Mail.SendMail(mail.EmailTo, mail.Title, mail.Text, mail.EmailFrom, mail.SenderName);

                        mqr.DeleteMail(mail.ID);
                        mqr.Save();
                    }
                    catch (Exception ex)
                    {
                        ErrorLog.GetDefault(null).Log(new Error(ex));
                    }
                }

            }
        }
    }
}